package com.twu.biblioteca;

import javafx.util.Pair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * Created by abhishekpuri on 10/7/14.
 */
public class Main {
    private static BibliotecaApp initializeBibliotecaApp() {
        Catalog<Book> inStockBooks = new Catalog<Book>();
        inStockBooks.addItemToCatalog(new Book("Book 1", "Author 1", new Date()));
        inStockBooks.addItemToCatalog(new Book("Book 2", "Author 2", new Date()));
        Catalog<Movie> inStockMovies = new Catalog<Movie>();
        inStockMovies.addItemToCatalog(new Movie("Movie 1",(short)2014,"Director 1"));
        inStockMovies.addItemToCatalog(new Movie("Movie 2",(short)2014,"Director 2",(short)7));
        Collection<User> users= new ArrayList<User>();
        users.add(new User("User 1","user1@mail.com","1234567890","pass".toCharArray(),"123-4567"));
        users.add(new User("User 2","user2@mail.com","1234567890","password".toCharArray(),"123-4568",(short)1));
        Collection<Pair<Book,User>> booksAllocatedToUsers = new ArrayList<Pair<Book, User>>();
        booksAllocatedToUsers.add(new Pair<Book, User>(new Book("Book 3", "Author 3", new Date()),(User)users.toArray()[0]));
        return new BibliotecaApp(inStockBooks,inStockMovies,users,booksAllocatedToUsers);
    }

    public static void main(String[] args) {
        BibliotecaApp ba=initializeBibliotecaApp();
        ArrayList<String> menuOptions = new ArrayList<String>();
        menuOptions.add("List Books");
        menuOptions.add("Checkout Book");
        menuOptions.add("Return Book");
        menuOptions.add("List Movies");
        menuOptions.add("Checkout Movie");
        menuOptions.add("Log Out");
        menuOptions.add("Quit");
        User loggedInUser=ba.userLogin();
        if(loggedInUser!=null) {
            if(loggedInUser.getAuthorityLevel()==1) {
                menuOptions.add("View checkouts by users");
            }
            Menu menu = new Menu(menuOptions);
            ba.printWelcomeMessage();
            int optionNumber = 0;
            while (loggedInUser!=null && optionNumber != menuOptions.indexOf("Quit")+1) {
                IOHandler ioHandler = new IOHandler();
                ioHandler.printlnToConsole("Hello " + loggedInUser.getUserSummary());
                menu.showMenuOptions();
                optionNumber = menu.readUserInput();
                if (optionNumber != menuOptions.indexOf("Quit")+1) {
                    ba.performSelectedOption(optionNumber);
                    loggedInUser=ba.getLoggedInUser();
                    if(loggedInUser==null) {
                        break;
                    }
                    else if(loggedInUser.getAuthorityLevel()==0 && menuOptions.contains("View checkouts by users")) {
                           menuOptions.remove("View checkouts by users");
                    }
                    else if(loggedInUser.getAuthorityLevel()==1 && !menuOptions.contains("View checkouts by users")) {
                        menuOptions.add("View checkouts by users");
                    }

                    ioHandler.printlnToConsole("Press enter to continue...");
                    try {
                        System.in.read();
                    } catch (IOException ex) {
                        ioHandler.printToErrorLog("BibAppErr : " + ex.getMessage());
                    }
                }
            }
        }
    }

}
